class Solution(object):
    def wordPattern(self, pattern, str):
        """
        :type pattern: str
        :type str: str
        :rtype: bool
        """
        strs = str.split(' ')
        if len(pattern) != len(strs):
            return False

        ptos_map = {}
        stop_map = {}

        for (i, c) in enumerate(pattern):
            if c not in ptos_map:
                ptos_map[c] = strs[i]
            else:
                if strs[i] != ptos_map[c]:
                    return False
            if strs[i] not in stop_map:
                stop_map[strs[i]] = c
            else:
                if c != stop_map[strs[i]]:
                    return False

        return True

s = Solution()
print(s.wordPattern("abba", "dog cat cat dog"))
print(s.wordPattern("abba", "dog dog dog dog"))
